#encoding=utf8
'''
Created on 2012-9-21

@author: Administrator
'''
from Processor import Processor
from models.RoomModel import RoomModel
from models.User import User
from models.UserModel import UserModel
from pub import Func, RoomDict
from pub.Config import Config
class RIClass(Processor):
    '''
    指令：UL(user login)
    备注：房间列表
    输入格式：cmd #cmdID#userID#startPosition#count
    输出格式：command#cmdID#roomId#keywords#title#desc#personNum....#keywords#title#desc#personNum
    '''
    def __init__(self,):
        Processor.__init__(self,)
        self.__outdata = None
        self.__field = {}
    def dataValidtor(self, cmdContent):
        if (len(cmdContent) == 3):            
            self.__field["userId"] = cmdContent[0]
            self.__field["startPosition"] = cmdContent[1]
            self.__field["count"] = cmdContent[2]
            return True;
        return False;
    def getRoomList(self,):
        '''
       房间列表
        '''
        returnValue = (Config.I_ROOM_LIST_ERROR, "", 0)
        roomList = RoomDict.getRoomList(self.__field["startPosition"], self.__field["count"]);
        
        if(roomList != None):
            tempstr = "";
            for oRoom in roomList.values():
#                print oRoom
#                print oRoom.get_first_name()
#                print oRoom.get_room_name()
#                print oRoom.get_introduce()
#                print oRoom.get_user_list()
#                print str(len(oRoom.get_user_list()))
                tempstr = tempstr + "#" + str(oRoom.get_id()) + "#" + oRoom.get_first_name() + "#" + oRoom.get_room_name() + "#" + oRoom.get_introduce() + "#" + str(len(oRoom.get_user_name_list()))+"#"+str(oRoom.get_upper_limit());
            tempstr = tempstr[1:]
            returnValue = (Config.I_ROOM_LIST_OK, tempstr, len(roomList))
        else:
            returnValue = (Config.I_ROOM_LIST_NULL, "", 0);
        return returnValue

    def outData(self):
        '''
        输出执行结果集
        '''
        (listState, listValue, counts) = self.getRoomList()
        result = Config.I_SYS_ZERO
#        UserId = 0
        
        if listState == Config.I_ROOM_LIST_OK:
            result = Config.I_SYS_ONE
#            UserId = self.__outdata.get_user_id()
        sock = self._sockObj
        Command = "%s#%s#%s#%s"
        CommandData = (self._cmd,
                       self._cmdId, counts, listValue,
        )
        Command = Command % CommandData
#        print Command
        self._sockObj.sendLine(str(Command))
        return Config.I_SYS_ONE;
